<template>
  <div ref="chart" style="height: 300px;width: 100%" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: ['checkBarList'],
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    checkBarList: {
      immediate: true,
      handler() {
        this.initChart()
      }
    }
  },
  methods: {
    initChart() {
      if (!this.$refs.chart) {
        return;
      }
      this.chart = echarts.init(this.$refs.chart)
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          data: this.checkBarList ? this.checkBarList.map(o => o.date) : []
        },
        yAxis: {},
        series: [
          {
            type: 'bar',
            data: this.checkBarList ? this.checkBarList.map(o => o.count) : []
          }
        ]
      })
    }
  }
}
</script>
